from flask import Flask,request

from mysql import TodoStoreWithMysql

app= Flask(__name__)

app.config['JSON_AS_ASCII']=False

manager=TodoStoreWithMysql()

@app.route('/')
def index():
    return '<h1>Hello Flask</h1>'
#app.add_url_rule('/',view_func=index)

@app.route('/todos',methods=['GET'])
def todos():
    return {'todos':[x.__dict__ for x in manager.get_list()]}

@app.route('/create',methods=['POST'])
def create_todo():
    # print(request.json)
    if 'title' not in request.json:
        return '参数错误，必须提供 title 参数',400
    todo =manager.create(request.json['title'])
    return todo.__dict__

@app.route('/mark_completed',methods=['GET'])
def mark_completed():
    if 'id' not in request.args:
        return '参数错误，必须提供 todo_id 参数',400
    id=int(request.args['id'])
    todo=manager.mark_completed(id)
    if todo is not None:
        return todo.__dict__
    else:
        return f'todo not found,id={id}',404
@app.route('/del_s',methods=['GET'])
def del_title():
    if 'id' not in request.args:
        return '参数错误，必须提供 todo_id 参数', 400
    id = int(request.args['id'])
    todo = manager.del_a(id)
    return todo.__dict__
if __name__=='__main__':
     app.run()
